package com.vcaresolution.logistic.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vcaresolution.logistic.form.ContactForm;
import com.vcaresolution.logistic.pojo.Codetable;
import com.vcaresolution.logistic.pojo.Contact;
import com.vcaresolution.logistic.to.ContactTO;
import com.vcaresolution.util.Utils;
//import com.vcaresolution.logistic.pojo.Codetable;
//import com.vcaresolution.logistic.pojo.Company;
//import com.vcaresolution.logistic.util.ApplicationConstants;

@Service
public class ContactServiceImpl extends BaseService implements ContactService {

	private Contact populateContactPojo(ContactForm contactForm) {
		Contact contact = new Contact();
		BeanUtils.copyProperties(contactForm, contact);

		return contact;
	}

	@Override
	@Transactional
	public void add(ContactForm contactForm) {
		Contact contact = populateContactPojo(contactForm);

		//contact.setCompany(getDaoFactory().getUtilityDAO().findById(Company.class, contactForm.getCompanyid()));
		contact.setCodetableByCategoryid(getDaoFactory().getUtilityDAO().findById(Codetable.class, contactForm.getCategoryid()));
		if( contactForm.getIndustryid() != null ){
			contact.setCodetableByIndustryid(getDaoFactory().getUtilityDAO().findById(Codetable.class, contactForm.getIndustryid()));
		}

		contact.setAuditdate(Utils.getCurrentDate());

		getDaoFactory().getUtilityDAO().add(contact);
	}

	@Override
	@Transactional
	public void update(ContactForm contactForm) {
		Contact contact = populateContactPojo(contactForm);

		//contact.setCompany(getDaoFactory().getUtilityDAO().findById(Company.class, contactForm.getCompanyid()));
		contact.setCodetableByCategoryid(getDaoFactory().getUtilityDAO().findById(Codetable.class, contactForm.getCategoryid()));
		if( contactForm.getIndustryid() != null ){
			contact.setCodetableByIndustryid(getDaoFactory().getUtilityDAO().findById(Codetable.class, contactForm.getIndustryid()));
		}
		
		contact.setAuditdate(Utils.getCurrentDate());

		getDaoFactory().getUtilityDAO().update(contact);
	}

	@Override
	@Transactional
	public void delete(ContactForm contactForm) {
		getDaoFactory().getUtilityDAO()
				.delete(populateContactPojo(contactForm));
	}

	@Override
	@Transactional
	public ContactTO findById(Serializable contactID) {
		Contact contact = (Contact) getDaoFactory().getUtilityDAO().findById(Contact.class, contactID);

		ContactTO contactTO = new ContactTO();
		BeanUtils.copyProperties(contact, contactTO);

		//contactTO.setCompanyid(contact.getCompany().getCompanyid());
		//contactTO.setCompanyname(contact.getCompany().getCompanyname());
		
		//contactTO.setCategoryid(contact.getCategory().getCodetableid());
		//contactTO.setCategoryname(contact.getCategory().getFormvalue());
		contactTO.setCategoryid(contact.getCodetableByCategoryid().getCodetableid());
		contactTO.setCategoryname(contact.getCodetableByCategoryid().getFormvalue());
		
		return contactTO;
	}

	@Override
	@Transactional
	public List<ContactTO> findByAll() {
		List<Contact> contactList = getDaoFactory().getUtilityDAO().findByAll(Contact.class);

		List<ContactTO> contactFormList = new ArrayList<ContactTO>();

		if (contactList != null && !contactList.isEmpty()) {
			ContactTO contactTO = null;
			for (Contact contact : contactList) {
				contactTO = new ContactTO();

				BeanUtils.copyProperties(contact, contactTO);

				// To show the Company Name
				//contactTO.setCompanyname(contact.getCompany().getCompanyname());
				// To show the Category Name
				contactTO.setCategoryname(contact.getCodetableByCategoryid().getFormvalue());
				
				
				contactFormList.add(contactTO);
			}
		}
		return contactFormList;
	}
}